package com.copm.ifm.servers.sys.controller;

import com.copm.ifm.base.basic.ServiceResponse;
import com.copm.ifm.base.service.base.BaseController;
import com.copm.ifm.servers.sys.entity.SysSetting;
import com.copm.ifm.servers.sys.service.SysSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 配置表相关接口类
 *
 * @author tp
 * @date 2021/1/4 17:39
 */
@Slf4j
@Api(tags = "系统设置/sys-setting")
@RestController
@RequestMapping("/sys-setting")
public class SysSettingController extends BaseController {

    @Autowired
    SysSettingService sysSettingService;

    @ApiOperation("查询-根据by type")
    @GetMapping("/by/type")
    public ServiceResponse<List<SysSetting>> getById(@ApiParam("type") @RequestParam String type) {
        return success(sysSettingService.selectByType(type));
    }

    @ApiOperation("保存-批量保存json串save")
    @PostMapping
    public ServiceResponse<?> save(@RequestParam String entityListStr) {
        sysSettingService.saveListStr(entityListStr);
        return success();
    }

    @ApiOperation("删除-根据by id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@RequestParam int id) {
        SysSetting sysSetting = sysSettingService.selectById(id);
        if (StringUtils.isEmpty(sysSetting)) {
            return fail("删除失败，无效数据");
        }
        sysSettingService.updateDeleted(id);
        return success();
    }

    @ApiOperation("查询select menuHead")
    @GetMapping("/menu/head")
    public ServiceResponse<Map<String,SysSetting>> selectMenuHead() {
        return success(sysSettingService.selectNavAndCSSHead());
    }
}